package com.ccx.web.backand;

import com.ccx.model.EbOrder;
import com.ccx.service.EbOrderService;
import com.github.pagehelper.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.List;



@Controller
@RequestMapping("/order")
public class EbOrderController {

    @Autowired
    EbOrderService ebOrderService;

    //显示分页
    @RequestMapping("/list")
    public String list(Model model, @RequestParam(value = "page",defaultValue = "1") Integer page, @RequestParam(value = "pageSize",defaultValue = "5") Integer paeSize){
        List<EbOrder> ebOrders = ebOrderService.pageTest(page, paeSize);
        model.addAttribute("ebOrders",ebOrders);

        model.addAttribute("page",page);
        model.addAttribute("maxPage",((Page)ebOrders).getPages());

        return "manage/order";
    }

    //根据ID查找对应的订单页面
    @RequestMapping("/to_edit")
    public String editOrder(Model model,int eoId){
        EbOrder ebOrder = ebOrderService.selectByOrder(eoId);
        model.addAttribute("ebOrder",ebOrder);
        return "manage/order-modify";
    }

    //修改订单页面
    @RequestMapping("/update")
    public String updateOrder(EbOrder ebOrder){
        int i = ebOrderService.updateTest(ebOrder);
        System.out.println(ebOrder.getEoId());
        if(i<1){
            return "manage/manage-error";
        }
        return "manage/manage-result";
    }

    //删除订单页面
    @RequestMapping("/delete")
    public String deleteOrder(int eoId){
        int i = ebOrderService.deleteTest(eoId);
        if(i<1){
            return "manage/manage-error";
        }
        return "manage/manage-result";
    }

    //根据订单号或姓名查找指定的信息
    @RequestMapping("/findByOrder")
    public String successOrder(Model model,
                               @RequestParam(value = "page",defaultValue = "1") Integer page,
                               @RequestParam(value = "pageSize",defaultValue = "5") Integer paseSize,
                               @RequestParam(value = "eoId",defaultValue = "0") int eoId,
                               @RequestParam(value = "eoUserName",defaultValue = "null") String eoUserName){
        //查找出ID和姓名的订单信息
        List<EbOrder> ebOrders1 = ebOrderService.selectSuccess(eoId, eoUserName,page,paseSize);

        model.addAttribute("ebOrders",ebOrders1);
        model.addAttribute("page",page);
        model.addAttribute("maxPage",((Page)ebOrders1).getPages());

        return "manage/order";
    }


}
